package nettyTest.netty3Test.decodetest.linebase;

import io.netty.bootstrap.Bootstrap;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.UnpooledByteBufAllocator;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.channel.ChannelOption;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;

public class LineBaseDecodeTestClient {

    public static void main(String[] args) {
        Bootstrap bootstrap = new Bootstrap();

        bootstrap.group(new NioEventLoopGroup()).channel(NioSocketChannel.class).handler(new LoggingHandler(LogLevel.DEBUG))
                .option(ChannelOption.TCP_NODELAY, true)
                .handler(new ChannelInboundHandlerAdapter() {
                    @Override
                    public void channelActive(ChannelHandlerContext ctx) {
                        ByteBuf firstMessage = UnpooledByteBufAllocator.DEFAULT.buffer();
                        firstMessage.writeInt(14);
                        firstMessage.writeShort(2);
                        firstMessage.writeBytes("\r\n".getBytes());
                        firstMessage.writeInt(3);
                        firstMessage.writeShort(4);
                        firstMessage.writeBytes("\r\n".getBytes());
                        firstMessage.writeInt(5);
                        ctx.writeAndFlush(firstMessage);
                    }
                });

        try {
            ChannelFuture sync = bootstrap.connect("127.0.0.1", 8000).sync();
            sync.channel().closeFuture().sync();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

    }
}
